package logic

import (
	"context"
	"errors"
	"shop/model/mysql"
	"shop/pkg"

	"shop/internal/svc"
	"shop/shop"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateuserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateuserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateuserLogic {
	return &UpdateuserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateuserLogic) Updateuser(in *shop.UpdateuserRequest) (*shop.UpdateuserResponse, error) {
	// todo: add your logic here and delete this line
	var err error
	var h mysql.House
	if err = h.GetByUserId(); err != nil {
		logx.Error("用户已经发布房源不能进行修改")
		return nil, errors.New("用户已经发布房源不能进行修改")
	}
	var u mysql.User
	if u.Password == pkg.Md5(in.Newpassword) {
		logx.Error("新旧密码不能一样")
		return nil, errors.New("新旧密码不能一样")
	}
	mysql.DB.Find(&u)
	u.Password = pkg.Md5(in.Newpassword)
	err = mysql.DB.Where("id = ?", u.ID).Updates(&u).Error
	if err != nil {
		logx.Error("修改失败")
		return nil, errors.New("修改失败")
	}
	return &shop.UpdateuserResponse{}, nil
}
